﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.Class;
using DevExpress.XtraGrid.Views.Grid;

namespace COMOSOFT.Production.Forms
{
    public partial class FrmProductionOutlinePlanningDaily : Form
    {
        ClassShare cls = new ClassShare("FrmProductionOutlinePlanningDaily");
        int Year, Month, OID, SubIID, SubFLID,Line;
        DataTable dtb;
        public FrmProductionOutlinePlanningDaily(object _Year, object _Month, object _OID, object _SubIID, object _SubFLID)
        {
            InitializeComponent();
            this.Year =Convert.ToInt32(_Year);
            this.Month = Convert.ToInt32(_Month);
            this.OID = Convert.ToInt32(_OID);
            this.SubIID = Convert.ToInt32(_SubIID);
            this.SubFLID = Convert.ToInt32(_SubFLID);
        }   
    
        #region[Load Data]
        public void LoadData()
        {
            try
            {
                sp_ProductionOutlinePlanningTableAdapter.Fill(this.dataSet_Production.sp_ProductionOutlinePlanning, Year, Month, OID, SubIID, SubFLID);               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Update Quantity]
        string Y, M, O, I, F, L;
        private void gridView_ProducionOP_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                GridView view = gridView_ProducionOP as GridView;
                Y = view.GetFocusedDataRow()["Year"].ToString();
                M = view.GetFocusedDataRow()["Month"].ToString();
                O = view.GetFocusedDataRow()["OID"].ToString();
                I = view.GetFocusedDataRow()["SubIID"].ToString();
                F = view.GetFocusedDataRow()["SubFLID"].ToString();
                L = view.GetFocusedDataRow()["Line"].ToString();
                Line = Convert.ToInt16(L);
                groupControl_DailyInfo.Text = "Daily Info: " + Month + "-" + Year + ", " + 
                                               "PO: "+view.GetFocusedDataRow()["PO"] + ", " +
                                               "Sub Item: "+view.GetFocusedDataRow()["SubItem"] + ", " + 
                                               "Sub Factory: "+view.GetFocusedDataRow()["SubFactoryName"]+", "+
                                               "Line: " + view.GetFocusedDataRow()["Line"];
                string Days = GetDays(Year, Month);
                dtb = cls.GetData("sp_ProductedQty", new string[] { "@Year", "@Month", "@OID", "@SubIID", "@SubFLID", "@Line", "@Days" }, 
                                                     new object[] {Y,M,O,I,F,L,Days});
                gridControl_DailInfo.DataSource = dtb;
                gridView_DailInfo.BestFitColumns();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Update Quantity]
        private void gridView_DailInfo_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                string Datetemp = Year + "-" + Month + "-"+e.Column.FieldName;
                DateTime date = Convert.ToDateTime(Datetemp);
                int Qty =Convert.ToInt32(e.Value);
                cls.UpdateData("sp_ProductedQtyUpdate", new string[] { "@Year", "@Month", "@OID", "@SubIID", "@SubFLID", "@Line", "@Date", "@Quantity" },
                                                        new object[] { Year, Month, OID, SubIID, SubFLID, Line, date, Qty });
                MessageBox.Show("Update Successfully!!");
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[GetDays]
        private string GetDays(int year,int month)
        {
            int TotalDay;
            string Days=null;
            if (month == 2)
            {
                  if ((year%4 == 0 && year%100 !=0) || year%400 == 0)
                      TotalDay = 29;
                  else
                      TotalDay = 28;
            }
            else
            {
                if (month == 4 || month == 6 || month == 9 || month == 11)
                    TotalDay = 30;
                else
                    TotalDay = 31;
            }
            Days="[1]";
            for (int i = 2; i <= TotalDay; i++)
            {
                Days += ",[" + i + "]";
            }
           return Days;
        }
        #endregion
    }
}